<template>
	<div class="content">
	    <div class="cloud-header">
	      <h2 class="r-title-h2">优惠管理</h2>
	    </div>
		<div class="cloud-body">
			<div class="r-member-level">
				<div class="r-member-title">等级优惠设置</div>
				<div class="r-level-item" v-for="item,index in level" :key="index">{{ item.name }} 联盟会员。在本店下单，可享受<NumberInput :precision="1" :min="0" :max="10" v-model="item.discount" v-width="200" />折优惠</div>
			</div>
			<Button color="primary" class="r-member-save"  @click="intSubmit"><i class="iconfont icon-save r-submit-icon"></i>保存</Button>
		</div>
	</div>
</template>
<script type="text/javascript">
	export default{
		data(){
			return {
				name:'user_level',
				userinfo:{},
				level:[{discount:''}],
			}
		},
		mounted(){
			this.userInfo = this.$cloud.userInfo();

 
			this.getLevel();
		},
		methods:{
			/**
			 * 获取会员等级
			 * @return {[type]} [description]
			 */
			async getLevel(){
				let fisrt = await this.$cloud.deploy().get(this.name);
				let vice = await this.$cloud.multi().get(this.userInfo._uniqueid);
				this.level = fisrt.data.data;
				this.level.map(item =>{
					if(vice){
						let data = {}
						for(let key in vice.level_setting){
							if(item['value'] == vice.level_setting[key]['value']){
								item['discount'] = vice.level_setting[key]['discount']
								data = item;
								break;
							}
						}
						return data;
					}else{
						item['discount'] = '';
						return item;
					}
				})
			},


			/**
			 * 等级优惠设置
			 * @return {[type]} [description]
			 */
			intSubmit(){
				let reg = false;
				this.level.forEach(item =>{
					if(item.discount == '' || item.discount == 0){
						console.log(item.discount)
						this.reg = true;
					}
				})
				if(this.reg){
					this.$Message.warn('请设置等级优惠！')
					return false;
				}

				this.$cloud.multi().edit(this.userInfo._uniqueid,{level_setting:this.level}).then(res =>{
					this.$Message.success('等级优惠设置成功！')
				}).catch(err =>{
					this.$Message.error("网络异常，请稍后重试！");
				})
			}
		}
	}
</script>
<style type="text/css" lang="less">
.r-member-level{
	border:solid 1px #dfdfdf;
	padding: 30px;
	position: relative;
	.r-level-item{
		display: flex;
		align-items: center;
		justify-content: flex-start;
		margin: 20px 0;
		input,.h-numberinput-show{
			margin: 0px 10px;
			text-align: center;
		}
	}
}
.r-member-save{
	margin-top: 20px;
}
.r-member-title{
	padding: 10px 20px;
	background-color: #fff;
	text-align: center;
	line-height: 20px;
	position: absolute;
	left:40px;
	top: -20px;
}
</style>